package com.x25175.file.controller;

import com.x25175.file.entity.Log;
import com.x25175.file.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 徐佳辉
 * @date 2020/11/30 10:28
 */
@Slf4j
@Component
@EnableScheduling
public class AutoController {

    /**
     * 一天的毫秒数
     */
    private static final int DAY = 1000 * 60 * 60 * 24;
    @Autowired
    private LogService logService;

    /**
     * 每天半夜12点删除前一天的记录日志
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void EveryDelete() {
        log.info("定时任务执行开始执行 : [{}]", new Date());

        List<Log> logs = logService.selectList();
        for (Log log1 : logs) {
            if (System.currentTimeMillis() - log1.getCreateTime().getTime() > DAY) {
                logService.delete(log1.getId());
            }
        }
        log.info("定时任务执行结束 : [{}]", new Date());


    }


}
